﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Acando.Dynamics.CRM.Silverlight.Library.Controls.FieldControls;
using Acando.Dynamics.CRM.Silverlight.Library.Interfaces;
using Acando.Dynamics.CRM.Silverlight.Library.Helper;
using Acando.Dynamics.CRM.Silverlight.Library.MetaSdk;
using Acando.Dynamics.CRM.Silverlight.Library.CrmSdk;

namespace RiaCrm.DynamicEntityGrid
{
    public class DynamicEntityDataGridColumn : DataGridColumn
    {

        private DynamicEntityGridViewModel Parent { get; set; }
        private string ControlType { get; set; }
        private string FieldName { get; set; }
        private EntityMetadata Metadata { get; set; }

        public DynamicEntityDataGridColumn(DynamicEntityGridViewModel parent, string controlType, string fieldname, string headername, EntityMetadata metadata, bool isReadOnly)
            : base()
        {
            this.Parent = parent;
            this.ControlType = controlType;
            this.FieldName = fieldname;
            this.Header = headername;
            this.Metadata = metadata;
            this.IsReadOnly = isReadOnly;
        }

        protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
        {
            return this.GenerateEditingElement(cell, dataItem);
        }

        protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem)
        {
            if (!this.Parent._recordControls.ContainsKey(dataItem))
                this.Parent._recordControls.Add(dataItem, new RecordControlWrapper() { Entity = dataItem as BusinessEntity, 
                                                                                       IsReadOnly = false,
                                                                                       Organame = Parent.Organame,
                                                                                       Metadatalayer = Parent.Metadatalayer
                                                                                       }); // record is general editable, not the switch for specific colums

            if (this.IsReadOnly)
                return ValueControlFactory.CreateControl(new RecordControlWrapper() { Entity = dataItem as BusinessEntity, 
                                                                                      IsReadOnly = true,
                                                                                      Organame = Parent.Organame,
                                                                                      Metadatalayer = Parent.Metadatalayer
                                                                                     }, this.Metadata, this.FieldName);
            else
                return ValueControlFactory.CreateControl(this.Parent._recordControls[dataItem], this.Metadata, this.FieldName);
                
        }

        protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs)
        {
            return null;
        }
    }
}
